package com.flashflexpro.tanksinbattle.battlescenetank {

import com.flashflexpro.tanksinbattle.battlescene.BattleScene;
import com.flashflexpro.tanksinbattle.battlescene.BattleSceneObject;
import com.flashflexpro.tanksinbattle.battlescene.dto.BattleSceneUserInputVO;
import com.flashflexpro.tanksinbattle.battlescenetank.dto.TankDto;
import com.flashflexpro.tanksinbattle.scene.AModule;
import com.flashflexpro.tanksinbattle.scene.dto.Dto;

import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

public class Tank extends BattleSceneObject  implements AModule{
    
    private var left:Boolean;
    private var right:Boolean;
    private var up:Boolean;
    private var down:Boolean;



    public function Tank( dto:TankDto, scene:BattleScene) {
        super( dto, scene );

        scene.hitting.push( this );
        scene.hitted.push( this );
    }

    public function start(dto:Dto):void {
    }

    public function get tankDto():TankDto{
        return _battleSceneObjDto as TankDto;
    }

    override public function update(now:int):void {
        super.update(now);

        var len:int = battleScene.commands.length;
        var lag:int = tankDto.commandLag;
        if( lag < 1 ){
            lag = 1;
        }
        var todo:BattleSceneUserInputVO;
        for( var i:int = 0; i< len; i ++ ){
            var cmd:BattleSceneUserInputVO = battleScene.commands[ i ] as BattleSceneUserInputVO;
            if( cmd.time == now - lag ){
                todo = cmd;
                break;
            }
        }
        if( todo != null ){
            trace( todo );
            if( todo.type == KeyboardEvent.KEY_DOWN || todo.type == KeyboardEvent.KEY_UP){

                var leftOrRight:int = 0;
                var upOrDown:int = 0;
                if( todo.keyCode == 37 || todo.keyCode == 39 ){
                    //left
                    if( todo.keyCode == 37 ){
                        left = ( todo.type == KeyboardEvent.KEY_DOWN );
                    }
                    //right
                    else if(todo.keyCode == 39){
                        right = ( todo.type == KeyboardEvent.KEY_DOWN );
                    }

                    //find out latest
                    if( left && right ) {
                        //left is the latest
                        leftOrRight = todo.keyCode == 37 ? -1 : 1;
                    }
                    else if( left || right ){
                        leftOrRight = left?-1:1;
                    }
                }
                else if( todo.keyCode == 38 || todo.keyCode == 40 ){
                    //up
                    if(todo.keyCode == 38){
                        up = ( todo.type == KeyboardEvent.KEY_DOWN );
                    }
                    //down
                    else if(todo.keyCode == 40){
                        down = ( todo.type == KeyboardEvent.KEY_DOWN );
                    }
                    if( up && down ) {
                        upOrDown = todo.keyCode == 38 ? -1 : 1;
                    }
                    else if( up || down){
                        upOrDown = up?-1:1;
                    }
                }
                trace( "^-v:" + upOrDown + "; <->:" + leftOrRight);
                var together:Boolean = Math.abs( upOrDown ) + Math.abs( leftOrRight ) > 1;
                if( upOrDown > 0 ){
                    velocity.y = together?tankDto.maxVelocity/Math.sqrt( 2 ):tankDto.maxVelocity;
                }
                else if( upOrDown < 0){
                    velocity.y = -( together?tankDto.maxVelocity/Math.sqrt( 2 ):tankDto.maxVelocity );
                }
                else{
                    velocity.y = 0;
                }

                if( leftOrRight > 0 ){
                    velocity.x = together?tankDto.maxVelocity/Math.sqrt( 2 ):tankDto.maxVelocity;
                }
                else if( leftOrRight < 0 ){
                    velocity.x = -( together?tankDto.maxVelocity/Math.sqrt( 2 ):tankDto.maxVelocity );
                }
                else{
                    velocity.x = 0;
                }
            }
            else if( todo.type == MouseEvent.MOUSE_DOWN || todo.type == MouseEvent.MOUSE_UP ){

            }
            else if( todo.type == MouseEvent.MOUSE_MOVE ){

            }
            else{
                throw new Error( "not supposed to be here!" );
            }
        }
        var canMove:Boolean = checkIfCanMoveFromHit();
        trace( "velocity[" + velocity.x + ", " + velocity.y + "]canMove:" + canMove);
        if( canMove && velocity.length > 0 ){
            x += velocity.x;
            y += velocity.y;
        }

        _hit = null;
    }


    public function destroy():void {
    }
}
}